package whf.framework.util;

import whf.framework.entity.Permissionable;
import whf.framework.security.UserContext;
import whf.framework.security.entity.Permission;

/**
 * @author wanghaifeng
 * @create Jan 26, 2007 9:01:20 AM
 * 
 */
public class PermissionUtils {
	
	/**
	 * 当所判定对象是IPermissionable并且用户上下文没有此对象的权限时返回false，
	 * 其它一律返回true
	 * @modify wanghaifeng Jan 26, 2007 9:01:26 AM
	 * @param userContext
	 * @param target 目标对象
	 * @return
	 */
	public static boolean hasPermission(UserContext userContext, Object target){
		if(target == null || userContext == null) 
			throw new IllegalArgumentException("UserContext and target object can't be null object!");
		if(target instanceof Permissionable){
			Permission perm = ((Permissionable)target).getPermission();
			if(perm != null && !userContext.hasPermission(perm)){
				return false;
			}
		}
		return true;
	}
	
	/**
	 * @modify wanghaifeng Jan 26, 2007 9:05:25 AM
	 * @param target
	 * @return
	 */
	public static boolean hasPermission(Object target){
		UserContext userContext = ThreadContext.getUserContext();
		return hasPermission(userContext, target);
	}
}
